pipeline {
	agent any
	
	parameters {
		string(name: 'branch', defaultValue: 'master', description: 'git branch')
	}
	
	stages{
		stage('同步源码') {
			steps {
				git url:'git@gitee.com:yivqin/newbell_mall.git', branch:'$branch'
			}
		}
		stage('前后端并行打包') {
			parallel {
				stage('后端打包') {
					steps {
						sh '''
							. /etc/profile
							cd newbee-mall-api-master
							mvn clean install -Dmaven.test.skip=true
							mvn clean package spring-boot:repackage -Dmaven.test.skip=true -U 
						'''
					}
				}
				stage('前端打包') {
					steps {
						sh '''
							source /etc/profile
							. /etc/profile
							cd newbee-mall-vue3-app-master
							npm install
							npm run build
						'''
					}
				}
			}
		}
		stage('清理环境') {
			steps {
				sh '''
					export pwd=`pwd`
					cd $pwd/temp
					rm -rf *
					cd /usr/local/nginx/www
					rm -rf dist
				'''
			}
		}
		stage('重新部署前后端') {
			parallel {
				stage('部署后端') {
					steps {
						sh '''
							. /etc/profile
							export pwd=`pwd`
							cp $pwd/newbee-mall-api-master/startup.sh ./temp
							cp $pwd/newbee-mall-api-master/step.sh ./temp
							cd temp
							touch logs.out
							mv $pwd/newbee-mall-api-master/target/newbee-mall-api-3.0.0-SNAPSHOT.jar .
							killjar(){
								export pid=`ps -ef|grep java|grep $1|awk '{print $2}'`
								if [ "${pid}" != "" ]
								then
									kill -9 $pid
								else
									echo '进程未运行'
								fi
							}
							killjar newbee-mall-api-3.0.0-SNAPSHOT.jar	
							docker restart mysql-8
							bash startup.sh
							bash step.sh
						'''
					}
				}
				stage('部署前端') {
					steps {
						sh '''
							. /etc/profile
							export pwd=`pwd`
							cd newbee-mall-vue3-app-master
							mv ./dist /usr/local/nginx/www
						'''
					}	
				}
			}
		}
	}
}